/*   1:    */ package org.jetbrains.ide;
/*   2:    */ 
/*   3:    */

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications.Bus;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.ShutDownTracker;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.io.BuiltInServer;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */ 
/*  26:    */ public class BuiltInServerManagerImpl
/*  27:    */   extends BuiltInServerManager
/*  28:    */ {
/*  29: 28 */   private static final Logger a = Logger.getInstance(BuiltInServerManager.class);
/*  30:    */   @NonNls
/*  31:    */   public static final String PROPERTY_RPC_PORT = "rpc.port";
/*  32:    */   private static final int b = 63342;
/*  33:    */   private static final int c = 20;
/*  34:    */   private volatile int d;
/*  35:    */   private final AtomicBoolean e;
/*  36:    */   @Nullable
/*  37:    */   private BuiltInServer f;
/*  38:    */   private boolean g;
/*  39:    */   
/*  40:    */   public BuiltInServerManagerImpl()
/*  41:    */   {
/*  42: 35 */     this.d = -1;
/*  43: 36 */     this.e = new AtomicBoolean(false);
/*  44:    */     
/*  45:    */ 
/*  46:    */ 
/*  47: 40 */     this.g = true;
/*  48:    */   }
/*  49:    */   
/*  50:    */   public int getPort()
/*  51:    */   {
/*  52: 44 */     return this.d == -1 ? a() : this.d;
/*  53:    */   }
/*  54:    */   
/*  55:    */   public BuiltInServerManager waitForStart()
/*  56:    */   {
/*  57: 49 */     Future localFuture = b();
/*  58: 50 */     if (localFuture != null)
/*  59:    */     {
/*  60: 51 */       a.assertTrue((ApplicationManager.getApplication().isUnitTestMode()) || (!ApplicationManager.getApplication().isDispatchThread()));
/*  61:    */       try
/*  62:    */       {
/*  63: 53 */         localFuture.get();
/*  64:    */       }
/*  65:    */       catch (InterruptedException localInterruptedException) {}catch (ExecutionException localExecutionException) {}
/*  66:    */     }
/*  67: 60 */     return this;
/*  68:    */   }
/*  69:    */   
/*  70:    */   private static int a()
/*  71:    */   {
/*  72: 64 */     return System.getProperty("rpc.port") == null ? 63342 : Integer.parseInt(System.getProperty("rpc.port"));
/*  73:    */   }
/*  74:    */   
/*  75:    */   static final class MyPostStartupActivity
/*  76:    */     implements StartupActivity, DumbAware
/*  77:    */   {
/*  78: 68 */     private boolean a = true;
/*  79:    */     
/*  80:    */     public void runActivity(@NotNull Project paramProject)
/*  81:    */     {
/*  82: 72 */       if (paramProject == null) {
/*  83: 72 */         throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/ide/BuiltInServerManagerImpl$MyPostStartupActivity", "runActivity" }));
/*  84:    */       }
/*  85: 72 */       if (!this.a) {
/*  86: 73 */         return;
/*  87:    */       }
/*  88: 76 */       this.a = false;
/*  89: 77 */       BuiltInServerManager localBuiltInServerManager = BuiltInServerManager.getInstance();
/*  90: 78 */       if ((localBuiltInServerManager instanceof BuiltInServerManagerImpl)) {
/*  91: 79 */         ((BuiltInServerManagerImpl)localBuiltInServerManager).b();
/*  92:    */       }
/*  93:    */     }
/*  94:    */   }
/*  95:    */   
/*  96:    */   private Future<?> b()
/*  97:    */   {
/*  98: 85 */     Application localApplication = ApplicationManager.getApplication();
/*  99: 86 */     if ((localApplication.isUnitTestMode()) && (!this.g)) {
/* 100: 87 */       return null;
/* 101:    */     }
/* 102: 90 */     if (!this.e.compareAndSet(false, true)) {
/* 103: 91 */       return null;
/* 104:    */     }
/* 105: 94 */     localApplication.executeOnPooledThread(new Runnable()
/* 106:    */     {
/* 107:    */       public void run()
/* 108:    */       {
/* 109: 97 */         int i = BuiltInServerManagerImpl.access$100();
/* 110: 98 */         int j = 1;
/* 111:100 */         if (Runtime.getRuntime().availableProcessors() > 1) {
/* 112:101 */           for (CustomPortServerManager localCustomPortServerManager : (CustomPortServerManager[])CustomPortServerManager.EP_NAME.getExtensions()) {
/* 113:102 */             if (localCustomPortServerManager.getPort() != i)
/* 114:    */             {
/* 115:103 */               j = 2;
/* 116:104 */               break;
/* 117:    */             }
/* 118:    */           }
/* 119:    */         }
/* 120:    */         try
/* 121:    */         {
/* 122:110 */           BuiltInServerManagerImpl.this.f = new BuiltInServer();
/* 123:111 */           BuiltInServerManagerImpl.this.d = BuiltInServerManagerImpl.this.f.start(j, i, 20, true);
/* 124:    */         }
/* 125:    */         catch (Exception localException)
/* 126:    */         {
/* 127:114 */           BuiltInServerManagerImpl.a.info(localException);
/* 128:115 */           String str = "Built-in Server";
/* 129:116 */           Notifications.Bus.register(str, NotificationDisplayType.STICKY_BALLOON);
/* 130:117 */           new Notification(str, "Internal HTTP server disabled", "Cannot start internal HTTP server. Git integration, JavaScript debugger and LiveEdit may operate with errors. Please check your firewall settings and restart " + ApplicationNamesInfo.getInstance().getFullProductName(), NotificationType.ERROR).notify(null);
/* 131:    */           
/* 132:    */ 
/* 133:    */ 
/* 134:121 */           return;
/* 135:    */         }
/* 136:124 */         if (BuiltInServerManagerImpl.this.d == -1)
/* 137:    */         {
/* 138:125 */           BuiltInServerManagerImpl.a.info("built-in server cannot be started, cannot bind to port");
/* 139:126 */           return;
/* 140:    */         }
/* 141:129 */         BuiltInServerManagerImpl.a.info("built-in server started, port " + BuiltInServerManagerImpl.this.d);
/* 142:    */         
/* 143:131 */         Disposer.register(ApplicationManager.getApplication(), BuiltInServerManagerImpl.this.f);
/* 144:132 */         ShutDownTracker.getInstance().registerShutdownTask(new Runnable()
/* 145:    */         {
/* 146:    */           public void run()
/* 147:    */           {
/* 148:135 */             if (!Disposer.isDisposed(BuiltInServerManagerImpl.this.f)) {
/* 149:137 */               Disposer.dispose(BuiltInServerManagerImpl.this.f);
/* 150:    */             }
/* 151:    */           }
/* 152:    */         });
/* 153:    */       }
/* 154:    */     });
/* 155:    */   }
/* 156:    */   
/* 157:    */   @Nullable
/* 158:    */   public Disposable getServerDisposable()
/* 159:    */   {
/* 160:148 */     return this.f;
/* 161:    */   }
/* 162:    */   
/* 163:    */   public void setEnabledInUnitTestMode(boolean paramBoolean)
/* 164:    */   {
/* 165:156 */     this.g = paramBoolean;
/* 166:    */   }
/* 167:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.ide.BuiltInServerManagerImpl

 * JD-Core Version:    0.7.0.1

 */